<template>
    <div class="container">
      <h2>丢失宠物信息</h2>

      <!-- 宠物信息列表 -->
      <table class="pet-list">
        <thead>
          <tr>
            <th>宠物名</th>
            <th>宠物种类</th>
            <th>宠物品种</th>
            <th>发布人</th>
            <th>联系方式</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="pet in filteredPets" :key="pet.id">
            <td>{{ pet.lossname }}</td>
            <td>{{ pet.losstype }}</td>
            <td>{{ pet.breed }}</td>
            <td>{{ pet.publisher }}</td>
            <td>{{ pet.contact }}</td>
            <td>
              <button @click="showDetails(pet)">详情</button>
              <button @click="deleteLosts(pet.id)">删除</button>
            </td>
          </tr>
        </tbody>
      </table>
      <!-- 详情弹窗 -->
      <div v-if="isDetailVisible" class="modal">
        <div class="modal-content">
          <span class="close" @click="closeDetail">&times;</span>
          <h3>宠物详情</h3>
          <p><strong>宠物名:</strong> {{ selectedPet.lossname }}</p>
          <p><strong>宠物种类:</strong> {{ selectedPet.losstype }}</p>
          <p><strong>宠物品种:</strong> {{ selectedPet.breed }}</p>
          <p><strong>丢失地区:</strong> {{ selectedPet.lostarea }}</p>
          <p><strong>丢失地点:</strong> {{ selectedPet.lostlocation }}</p>
          <p><strong>宠物描述:</strong> {{ selectedPet.lossdescription }}</p>
          <p><strong>发布人:</strong> {{ selectedPet.publisher }}</p>
          <p><strong>联系方式:</strong> {{ selectedPet.contact }}</p>
        </div>
      </div>
      <div v-if="deletedLoseMessage" class="message">{{ deletedLoseMessage }}</div>
      <div v-if="filteredPets.length === 0">
        <p>目前没有符合条件的宠物丢失信息。</p>
    </div>
    </div>
  </template>
  
  <script>
  import { deleteLost, findAllLost } from "../../api/admins";
  export default {
    data() {
      return {
        searchQuery: "", 
        pets: [ 

        ],
        selectedPet: null, 
        isDetailVisible: false, 
        deletedLoseMessage: ''
      };
    },
    computed: {
      // 根据搜索框内容过滤宠物列表
      filteredPets() {
        return this.pets.filter(pet => pet.lossname.toLowerCase().includes(this.searchQuery.toLowerCase()));
      }
    },
    methods: {
      async deleteLosts( id){
        try {
          const response = await deleteLost(id);
          if (response.data) {
            this.fetchPets();
          }
        } catch (error) {
          console.error('Error deleting pet:', error);
        }
      },
      async fetchPets() {
        try {
          const response = await findAllLost();
          this.pets = response.data;
        } catch (error) {
          console.error("Error fetching pets:", error);
        }
      },
      showDetails(pet) {
        this.selectedPet = pet;
        this.isDetailVisible = true;
      },
      closeDetail() {
        this.isDetailVisible = false;
        this.selectedPet = null;
      },
      confirmDelete(pet) {
        const confirmation = confirm('是否确认删除该公告?');
        if (confirmation) {
          this.deletePet(pet);
        }
      },
      deletePet(pet) {
        const index = this.pets.findIndex(p => p.id === pet.id);
        if (index !== -1) {
            this.pets.splice(index, 1);
            this.deletedLoseMessage = `丢失宠物信息已删除！`;
            // 2秒后清除提示信息
            setTimeout(() => {
              this.deletedLoseMessage = '';
            }, 2000);
         }
      },

    },

    mounted() {
      this.fetchPets();
    }
  };
  </script>
  
<style scoped>
.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 40px;
  font-family: 'Arial', sans-serif;
  background-color: #f7f7f7;
}
h2 {
    font-size: 24px;
    color: #333;
    text-align: center;
    margin-bottom: 20px;
}
  
.search-box {
  display: flex;
  justify-content: center;
  margin-bottom: 20px;
}
.search-box input {
  padding: 10px;
  width: 300px;
  font-size: 1rem;
  border: 1px solid #ddd;
  border-radius: 5px;
  transition: border-color 0.3s ease;
}
.search-box input:focus {
  border-color: #5b9bd5;
  outline: none;
}
  
.pet-list {
  width: 100%;
  border-collapse: collapse;
  margin-top: 20px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
tr:hover {
    background-color: #fafafa;
  }
th, td {
  padding: 12px 15px;
  text-align: left;
  border-bottom: 1px solid #ddd;
}
th {
  background-color: #f4f4f4;
  cursor: pointer;
  font-weight: bold;
  position: relative;
}
th:hover {
    background-color: #e2e2e2;
  }
th span {
  position: absolute;
  right: 10px;
  font-size: 14px;
} 
button {
  padding: 6px 12px;
  background-color: #4CAF50;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  transition: background-color 0.3s ease;
  margin-right: 10px;
}
button:hover {
  background-color: #45a049;
}
button:disabled {
  background-color: #dcdcdc;
  cursor: not-allowed;
}
button:last-child {
    margin-right: 0; 
}
  
.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    justify-content: center;
    align-items: center;
}
.modal-content {
    background-color: #fff;
    padding: 20px;
    border-radius: 8px;
    width: 500px;
    max-width: 90%;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    position: relative;
}
.modal h3 {
  text-align: center;;
}
.modal-content p {
    margin: 8px 0;
    text-align: left;
}
.close {
    position: absolute;
    top: 5px;
    right: 10px;
    font-size: 30px;
    cursor: pointer;
    color: #333;
}

.message {
  color: green;
  font-size: 1rem;
  text-align: center;
  margin-top: 20px;
}
  </style>